package com.hunttown.mes.service;

import com.hunttown.common.domain.Query;
import com.hunttown.mes.dao.AnalysisManageLoginLogDao;
import com.hunttown.mes.domain.AnalysisManageLoginLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * created by wangjunfu on 2021-02-25 10:27:41
 */
@Service
public class AnalysisManageLoginLogService {

    private final static Logger logger = LoggerFactory.getLogger(AnalysisManageLoginLogService.class);

    private final AnalysisManageLoginLogDao analysisManageLoginLogDao;
    private final AHelpService helpService;

    @Autowired
    public AnalysisManageLoginLogService(AnalysisManageLoginLogDao analysisManageLoginLogDao, AHelpService helpService) {
        this.analysisManageLoginLogDao = analysisManageLoginLogDao;
        this.helpService = helpService;
    }

    /**
     * 新增
     *
     * @param analysisManageLoginLog 实体对象
     * @return 最新插入id
     */
    public AnalysisManageLoginLog insert(AnalysisManageLoginLog analysisManageLoginLog) {
        //api接口传进来是有值的
        if (analysisManageLoginLog.getEntId() == null)
            analysisManageLoginLog.setEntId(helpService.getEnterpriseId());
        if (analysisManageLoginLog.getAdminId() == null)
            analysisManageLoginLog.setAdminId(0);
        if (analysisManageLoginLog.getDeleteFlag() == null)
            analysisManageLoginLog.setDeleteFlag(0);

        return analysisManageLoginLogDao.insert(analysisManageLoginLog);
    }

    /**
     * 根据ID修改数据信息
     *
     * @param analysisManageLoginLog 实体对象
     * @return True or False
     */
    public Boolean updateInfoById(AnalysisManageLoginLog analysisManageLoginLog) {
        if (analysisManageLoginLog.getEntId() == null) {
            analysisManageLoginLog.setEntId(helpService.getEnterpriseId());
        }
        return analysisManageLoginLogDao.updateInfoById(analysisManageLoginLog);
    }

    /**
     * 根据Query修改数据信息
     *
     * @param query 需要修改的字段 KV 格式
     * @return True or False
     */
    public Boolean updateInfoByQuery(Query query) {
        if (!query.containsKey("w_entId")) {
            query.put("w_entId", helpService.getEnterpriseId());
        }
        return analysisManageLoginLogDao.updateInfoByQuery(query);
    }

    /**
     * 根据ID获取记录信息
     *
     * @param id 主键id
     * @return 实体对象
     */
    public AnalysisManageLoginLog selectById(Integer id) {
        return analysisManageLoginLogDao.selectById(id, helpService.getEnterpriseId());
    }

    /**
     * 根据query取出一条记录，但是可能有多条记录，取最新的一条。
     *
     * @param query 查询条件 KV 格式
     * @return 实体对象
     */
    public AnalysisManageLoginLog selectObjByQuery(Query query) {
        if (query == null || query.size() == 0) {
            return null;
        }
        if (!query.containsKey("entId")) {
            query.put("entId", helpService.getEnterpriseId());
        }

        List<AnalysisManageLoginLog> list = selectByQuery(query);
        if (list == null || list.size() == 0) {
            return null;
        }

        return list.get(0);
    }

    /**
     * 根据条件获取List
     *
     * @param query 查询条件 KV 格式
     * @return 实体对象列表
     */
    public List<AnalysisManageLoginLog> selectByQuery(Query query) {
        if (!query.containsKey("deleteFlag")) {
            query.put("deleteFlag", 0);
        }
        if (!query.containsKey("entId")) {
            query.put("entId", helpService.getEnterpriseId());
        }

        return analysisManageLoginLogDao.selectByQuery(query);
    }

    /**
     * 根据条件获取记录总数
     *
     * @param query 查询条件 KV 格式
     * @return 总记录数
     */
    public Long countByQuery(Query query) {
        if (!query.containsKey("deleteFlag")) {
            query.put("deleteFlag", 0);
        }
        if (!query.containsKey("entId")) {
            query.put("entId", helpService.getEnterpriseId());
        }

        return analysisManageLoginLogDao.countByQuery(query);
    }

    /**
     * 根据ID删除记录信息
     *
     * @param id 主键id
     * @return True or False
     */
    public Boolean deleteById(Integer id) {
        return analysisManageLoginLogDao.deleteById(id, helpService.getEnterpriseId());
    }
}